package utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JTextArea;

public class TextPrinter {

  private JTextArea targetComponent;
  private static TextPrinter instance;

  private TextPrinter() {
  }

  public static TextPrinter getInstance() {
    if (instance != null) {
      return instance;
    } else {
      instance = new TextPrinter();
      return instance;
    }
  }

  public void setTargetComponent(JTextArea jTextArea) {
    this.targetComponent = jTextArea;
  }

  public void appendString(String str) {
    if (targetComponent != null) {
      targetComponent.append(str + " ");
    } else {
      System.out.print(str + " ");
    }
  }

  public void printLine(String str) {
    if (targetComponent != null) {
      targetComponent.append(new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()) + " " + str + "\n");
    } else {
      System.out.println(new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()) + " " + str);
    }
  }
  
  public void print(String str) {
	    if (targetComponent != null) {
	      targetComponent.append(str );
	    } else {
	      System.out.print(str);
	    }
	  }

  public void printLine() {
    if (targetComponent != null) {
      targetComponent.append("\n");
    } else {
      System.out.println();
    }
  }

  public void printLine(String[] strtab) {
    if (targetComponent != null) {
      for (String str : strtab) {
        targetComponent.append(str + " ");
      }
      targetComponent.append("\n");
    } else {
      for (String str : strtab) {
        System.out.print(str + " ");
      }
      System.out.print("\n");
    }

  }

}
